#ifndef GPUBLIC_H
#define GPUBLIC_H

#include <QString>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _X86_
typedef unsigned char uint8_t;
// typedef unsigned short uint16_t;
// typedef unsigned int uint32_t;
typedef unsigned char byte;
// typedef unsigned short uint16_t;
// typedef unsigned int uint32_t;
// typedef long long uint64_t;

// typedef char int8_t;
// typedef short int16_t;
// typedef int int32_t;

typedef unsigned char u8;
typedef unsigned char byte;
typedef unsigned short u16;
typedef unsigned int u32;

#ifdef BOOL
#undef BOOL
#endif
typedef int BOOL;

typedef char s8;
typedef short s16;
typedef int s32;
#else
typedef unsigned char u8;
typedef unsigned char byte;
typedef unsigned short u16;
typedef unsigned int u32;

typedef int BOOL;
typedef char s8;
typedef short s16;
typedef int s32;
#endif

#define getarraylen(array) (int)(sizeof(array) / sizeof(array[0]))

#define unsed(A)                                                               \
  do {                                                                         \
    (void)(A);                                                                 \
  } while (0)

#define uSucess (0)
#define uError (1)
#define uNotFound (2)
#define true 1
#define false 0
#define maxBuffLen (4096)
#define d645MaxBuffLen (512)
#define AddrMaxlen 6
#define minFramLen 12
/* 电表地址长度 */
#define AddrMaxLen 6
#define DiMaxLen 4
#define GQScanTimes 3
#define rtkFrameNoDataLen (21)
#define rtkMinFrameLen 22
#define rtkPairDevCount 5
#define rtkPairMainCount 2
#define rtkFeMaxLength (4)
/* 过滤列表最大存储32设备 */
#define filterDevicesMax (0x14)
#define filterTypeMax (0x14)
#define advInfoMaxLen (0x1F)

/* 定义软件版本号 */
#define v_major 1    /* 主版本号 0~15 */
#define v_minor 1    /* 从版本号 0~127 */
#define v_revision 0 /* 修订版本 15 bit位 */
#define v_reserve 0  /* 保留  0~63*/

/* 定义串口号 */
#define logUart HS_UART0
#define transUart HS_UART1

/* 定义串口管脚 */
//[传输]
#define trans_rx DATA_PIN_UART1_RX /* 接收管脚 */
#define trans_tx DATA_PIN_UART1_TX /* 发送管脚 */
//[调试日志]
#define logs_rx DEBUG_PIN_UART0_RX /* 接收管脚 */
#define logs_tx DEBUG_PIN_UART0_TX /* 发送管脚 */

/*  定义功能管脚 */
#define GPIO_QA_5 20 /* 默认GPIO */
#define GPIO_QB_6 24 /* 默认GPIO */
//#define GPIO_RST_7 0 /* 硬件复位复位管脚 */
#define GPIO_STA_8 22  /* 状态管脚 */
#define GPIO_MODE_9 9  /*工作模式管脚 */
#define GPIO_PLUSE_A 8 /* 有功脉冲管脚 */
#define GPIO_MFUN_B 7  /* 多功能脉冲管脚 */

/* 定义打印等级 测试使用 */
#define debugLv2 1

#define logInfo printf

#ifdef debugLv1
#define logDebug(format, ...) printf(format, ##__VA_ARGS__)
#elif debugLv2
#define logDebug(format, ...)                                                  \
  (printf("%s[%d]: ", __FILE__, __LINE__), printf(format, ##__VA_ARGS__))
#elif debugLv3
#define logDebug(format, ...)                                                  \
  (printf("%s[%d]-<%s>: ", __FILE__, __LINE__, __FUNCTION__),                  \
   printf(format, ##__VA_ARGS__))
#endif

#define logDebugArray(array, len)                                              \
  do {                                                                         \
    int __i;                                                                   \
    for (__i = 0; __i < (len); ++__i)                                          \
      logInfo("%02X ", ((u8 *)(array))[__i]);                                  \
  } while (0)
#define logDebugArrayEx(note, array, len)                                      \
  do {                                                                         \
    logDebug("%s[%d]: ", note, len);                                           \
    logDebugArray(array, len);                                                 \
    logInfo("\n");                                                             \
  } while (0)

/* 字节序调换 */
#define BSWAP_8(x) ((x)&0xff)
#define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
#define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
#define BSWAP_64(x) ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))

/* 内存管理相关函数 */
#ifdef _X86_
#define GQ_IOT_Malloc malloc
#define GQ_IOT_free free
#define GQ_IOT_Memcpy memcpy
#define GQ_IOT_Memmove memmove
#define GQ_IOT_Memcmp memcmp
#define GQ_IOT_Memset memset
#elif GQ_MEM
#define GQ_IOT_Malloc co_malloc
#define GQ_IOT_free co_free
#define GQ_IOT_Memcpy memcpy
#define GQ_IOT_Memmove memmove
#define GQ_IOT_Memcmp memcmp
#define GQ_IOT_Memset memset
#endif

static QString version = "v1.1.2A";

#endif // GPUBLIC_H
